<template>
  <div>
    <el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        v-for="(item, index) in propsArr"
        :key="item + index"
        :prop="item.prop"
        :label="item.label"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getTreatResult } from "@/api/treatProcess";
export default {
  name: "Record",
  created() {
    window.app = this;
    getTreatResult({}).then(response => {
      if (response.code === 200) {
        this.dataList = {};
        /*response.page.filter(function(a) {
          if (!app.dataList[a.clinicNo]) {
            app.dataList[a.clinicNo] = [];
          }
          app.dataList[a.clinicNo].push(a);
        });*/

        response.page.filter(function(a) {
          if (!app.dataList[a.clinicNo]) {
            app.dataList[a.clinicNo] = {
              clinicNo: a.clinicNo,
              projectName: a.projectName,
              patientName: a.patientName,
              doctorName: a.doctorName,
              deptName: a.deptName,
              projectId: a.projectId,
              patientId: a.patientId,
              children: []
            };
          }
          app.dataList[a.clinicNo].children.push({
            id: a.clinicNo,
            indicatorsActVal: a.indicatorsActVal,
            indicatorsId: a.indicatorsId,
            indicatorsName: a.indicatorsName,
            indicatorsReferVal: a.indicatorsReferVal,
            inspectId: a.inspectId,
            inspectName: a.inspectName,
            parentInfoName: a.parentInfoName,
            parentItemName: a.parentItemName,
            remark: a.remark,
            seq: a.seq
          });
        });

        this.tableData = response.page;
      }
    });
  },
  data() {
    return {
      dataList: {},
      tableData: [],
      /* clinicId: 1
clinicNo: "123"
createBy: "admin"
createDate: "2020-04-02T11:46:15.000+0000"
deptId: 1
deptName: "信息部门"
doctorId: 1
doctorName: "元康"
id: 55
indicatorsActVal: "indicatorsActVal"
indicatorsId: 26
indicatorsName: "NRA"
indicatorsReferVal: "0"
inspectId: 18
inspectName: "相对调节"
lastUpdateBy: null
lastUpdateDate: "2020-04-02T11:46:15.000+0000"
parentInfoName: null
parentItemName: "OD;"
patientId: 1
patientName: "caine"
projectId: 12
projectName: "预检"
remark: null
seq: 0
unit: null */
      // 需要合并的属性
      neendCatArray: ["patientName", "clinicNo", "projectName", "inspectName"],
      // 表属性和属性名
      propsArr: [
        {
          prop: "patientName",
          label: "患者姓名"
        },
        {
          prop: "clinicNo",
          label: "门诊号"
        },
        {
          prop: "projectName",
          label: "项目"
        },
        {
          prop: "inspectName",
          label: "分项目"
        },
        {
          prop: "indicatorsActVal",
          label: "检测数值"
        },
        {
          prop: "indicatorsReferVal",
          label: "参考范围"
        }
      ]
    };
  },
  methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (this.neendCatArray.includes(column.property)) {
        let data = this.tableData;
        if (
          rowIndex > 0 &&
          data[rowIndex][column.property] ===
            data[rowIndex - 1][column.property]
        ) {
          return {
            rowspan: 0,
            colspan: 0
          };
        } else {
          let rowNum = 1;
          let columnNum = 1;
          for (let i = rowIndex; i < data.length - 1; i++) {
            if (
              data[rowIndex][column.property] !==
              data[rowIndex + 1][column.property]
            ) {
              break;
            }
            rowNum++;
          }
          return {
            rowspan: rowNum,
            colspan: columnNum
          };
        }
      } else {
        return {
          rowspan: 1,
          colspan: 1
        };
      }
    }
  }
};
</script>
